package com.anlogic.sdk.bootimage.internal.ui.common;

import com.anlogic.sdk.bootimage.model.options.IBootgenOption;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;

public class BootgenComboViewerUtils {
	public static IBootgenOption getSelection(ComboViewer comboViewer) {
		return getSelection(comboViewer, IBootgenOption.class);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getSelection(ComboViewer comboViewer, Class<T> clazz) {
		ISelection selection = comboViewer.getSelection();
		if (selection instanceof IStructuredSelection) {
			Object firstElement = ((IStructuredSelection) selection).getFirstElement();
			if (firstElement != null && firstElement.getClass().isAssignableFrom(clazz))
				return (T) firstElement;
		}
		return null;
	}
}
